<?php
require_once dirname(dirname(dirname(__FILE__))) .'/cse-init.php';

// authentication is required!
Cinnamon::registry('session')->applyAuthenticationPolicy();

// load required classes
Cinnamon::loadClass('TabbedPane');
Cinnamon::loadClass('DataGrid');
Cinnamon::loadClass('Text');

// fetch required object from registry
$view = Cinnamon::registry('view');
$db = Cinnamon::registry('db');


// Referrers Tabbed Pane
$referrers = new TabbedPane('Referrers');

// Referrers > Most recent
$dg = new DataGrid('Most recent');
$dg->pagination = true;
$dg->addColumn('From', 'function', 'referrersLabelFunction');
$dg->addColumn('When', 'field', 'request_datetime', 'width', 100);

$dp = $db->requests('request_url, request_title, referrer_url, referrer_title, request_datetime')
   		 ->filter('referrer_url <> ?', null)
   		 ->filter('search_terms = ?', null)
   		 ->sort('request_datetime', 'DESC');
$dg->setDataProvider($dp);

$referrers->addChild($dg);

// Referrers > Most popular
$dg = new DataGrid('Most popular');
$dg->pagination = true;
$dg->addColumn('From', 'function', 'referrersLabelFunction');
$dg->addColumn('Visits', 'field', 'visits', 'width', 50);

$dp = $db->requests('COUNT(request_id) visits, referrer_url, referrer_title')
   		 ->filter('referrer_url <> ?', null)
   		 ->filter('search_terms = ?', null)   
   		 ->group('referrer_url, referrer_title')
   		 ->sort('visits', 'DESC');
$dg->setDataProvider($dp);

$referrers->addChild($dg);


// Referrers label function
function referrersLabelFunction($item) {
	$request_title 	= nvl($item->request_title, $item->request_url);
	$referrer_title = nvl($item->referrer_title, $item->referrer_url);
	
	$from 	= '<a href="'. $item->referrer_url .'" title="'. $referrer_title .'">'. $referrer_title .'</a>';
	$to 	= 'To: <a href="'. $item->request_url .'" title="'. $request_title .'">'. $request_title .'</a>';
	
	if ( isset($item->request_url) ) {
		return Cinnamon::registry('formats')->format($from, 'nowrap') . Cinnamon::registry('formats')->format($to, 'nowrap');
	} else {
		return Cinnamon::registry('formats')->format($from, 'nowrap');
	}
}

// Search terms tabbedpane
$search = new TabbedPane('Search terms');

// Search terms > Most recent
$dg = new DataGrid('Most recent');
$dg->pagination = true;
$dg->addColumn('For', 'function', 'searchLabelFunction');
$dg->addColumn('When', 'field', 'request_datetime', 'width', 100);

$dp = $db->requests('request_url, request_title, referrer_url, search_terms, request_datetime')
   		 ->filter('referrer_url <> ?', null)
   		 ->filter('search_terms <> ?', null)
   		 ->sort('request_datetime', 'DESC');
$dg->setDataProvider($dp);

$search->addChild($dg);

// Search terms > Most popular
$dg = new DataGrid('Most popular');
$dg->pagination = true;
$dg->addColumn('For', 'function', 'searchLabelFunction');
$dg->addColumn('Visits', 'field', 'visits', 'width', 50);

$dp = $db->requests('COUNT(request_id) AS visits, referrer_url, search_terms')
   		 ->filter('referrer_url <> ?', null)
   		 ->filter('search_terms <> ?', null)
   		 ->group('search_terms, referrer_url')
   		 ->sort('visits', 'DESC');
$dg->setDataProvider($dp);

$search->addChild($dg);

// Search terms label function
function searchLabelFunction($item) {
	$request_title 	= nvl($item->request_title, $item->request_url);
	$referrer_title = nvl($item->search_terms, $item->referrer_url);
	
	$from 	= '<a href="'. $item->referrer_url .'" title="'. $referrer_title .'">'. $item->search_terms .'</a>';
	$to 	= 'To: <a href="'. $item->request_url .'" title="'. $request_title .'">'. $request_title .'</a>';
	
	if ( isset($item->request_title) ) {
		return Cinnamon::registry('formats')->format($from, 'nowrap') . Cinnamon::registry('formats')->format($to, 'nowrap');
	} else {
		return Cinnamon::registry('formats')->format($from, 'nowrap');
	}
}


// Render output
//$view->renderHeader();
$referrers->render();
$search->render();
//$view->renderFooter();
?>